#include "../inc/ec11_driver.h"

void ec11_init(void)
{
	P3M1 |=  0x30;
	P4M1 |=  0x20;
}

unsigned char ec11_scan(void)
{
	static unsigned char step_cnt = 0;
	unsigned char value = 0;
	switch(step_cnt)
	{
		case 0:
			if(EC11_A && EC11_B)
				step_cnt ++;
			break;
		case 1:
			if((!EC11_A) && EC11_B)
				step_cnt ++;
			else if(EC11_A && (!EC11_B))
				step_cnt += 2;
			break;
		case 2:
			if((!EC11_A) && EC11_B)
				step_cnt += 2;
			else
				step_cnt = 0;
			break;
		case 3:
			if(EC11_A && (!EC11_B))
				step_cnt += 2;
			else
				step_cnt = 0;
			break;
		case 4:
			if(EC11_A && EC11_B)
			{
				step_cnt = 0;
				value = 1;
			}
			break;
		case 5:
			if(EC11_A && EC11_B)
			{
				step_cnt = 0;
				value = 2;
			}
			break;
	}
	return value;
}

unsigned char ec11_key_scan(void)
{
	static unsigned char t = 0;
	if(EC11_KEY)
	{
		t = 0;
		return 0;
	}
	else
	{
		t ++;
		if(t == 20)
		{
			t &= 0x7f;
			return 3;
		}
	}
	return 0;
}

